package com.ljw.i18demo.configuration;

import org.hibernate.validator.BaseHibernateValidatorConfiguration;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Locale;

/**
 * @Author LiuJunWen
 * @Date 2020/5/20 23:46
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    @Autowired
    private MessageSource messageSource;

    /**
     * 默认解析器 其中locale表示默认语言
     *
     * @return
     */
    @Bean
    public SessionLocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
        localeInterceptor.setParamName("lang");
        registry.addInterceptor(localeInterceptor);
    }

//    /**
//     * 指定国际化文件路径，及其其他配置
//     * 与localValidatorFactoryBean()方法互斥
//     * @return
//     */
//    @Bean
//    public Validator validator() {
//        String bundleName = "i18n/messages";
//        Validator validator = Validation.byDefaultProvider()
//                .configure()
////                .messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator
////                (bundleName)))  // 直接指定国际化文件路径
//                .messageInterpolator(new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource)))// 直接指定已有的messageSource
//                .addProperty(BaseHibernateValidatorConfiguration.FAIL_FAST, "true")// 是否快速结束校验过程，只要遇到校验失败后，即结束校验
//                .buildValidatorFactory()
//                .getValidator();
//        return validator;
//    }

    /**
     * 指定国际化文件路径，及其其他配置
     * 参考：org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration#defaultValidator()
     * @return
     */
    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean() {
        LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
        validatorFactoryBean.setValidationMessageSource(this.messageSource);
        // 是否快速结束校验过程，只要遇到校验失败后，即结束校验
        validatorFactoryBean.getValidationPropertyMap().put(BaseHibernateValidatorConfiguration.FAIL_FAST, "true");
        return validatorFactoryBean;
    }
}
